<?php
include_once("formInputs.php");

function generaFormWindowJS($meta){

$atributs = $meta->getAtributs();
$atributs = explode(" ,", $atributs);
$tipus = $meta->getTipus();
$tipus = explode(" ,", $tipus);

$taula = $meta->getTaula();
$pk = $meta->getPk();
$idiomes = $meta->getNumIdiomes();

$variable = "
Ext.ns('Application');

//INICIO CLASE
".$taula."FormWindow = Ext.extend(Ext.Window, {
	title: '$taula',
	width: 640,
	height: 480,
	layout: 'fit',
	closeAction: 'hide',
	 
	initComponent:function() {
	
		this.grid = undefined;
		
		this.accio = new Ext.form.TextField
		({
			fieldLabel: 'accio',
			name: 'accio',
			width: '100',
			maskRe: /([a-zA-Z0-9\s]+)$/,
			hideMode: 'offsets',
			hideLabel: true,
			hidden: true
		});
";
			
		for( $i=0; $i<count($atributs); $i++ ){
			//PK o ID2 o IDIOMA
			if( $atributs[$i] == $pk or $atributs[$i] == "id2" or $atributs[$i] == "idioma" ){
				$variable .= generaFormNumberField( "$atributs[$i]", "$atributs[$i]", 60, "Number.MAX_VALUE", 0, 0, 1, 1);
			}else{
				//IMG
				if( substr ($atributs[$i], 0, 4) == "img_" or substr ($atributs[$i], 0, 5) == "file_" ){
					$variable .= generaFormImgField( "$atributs[$i]", "$atributs[$i]", 300, 1, 0);
				//ALTRES
				}else{
					for( $j=1; $j<=$idiomes; $j++ ){
						switch ( $tipus[$i] ){					
							case 'string':
								$variable .= generaFormTextField( "$atributs[$i]$j", "$atributs[$i]$j", 300, "Number.MAX_VALUE", 1, 0);
							break;
							case 'blob':
								$variable .= generaFormHtmlEditor( "$atributs[$i]$j", "$atributs[$i]$j", 470, 150, 0);
							break;
							case 'time':
								$variable .= generaFormTimeField( "$atributs[$i]$j", "$atributs[$i]$j", 100, "H:i", 30, 1, 0 );
							break;
							case 'date':
								$variable .= generaFormDateField( "$atributs[$i]$j", "$atributs[$i]$j", 100, 'd/m/Y', 1, 0);
							break;
							case 'int':
								$variable .= generaFormNumberField( "$atributs[$i]$j", "$atributs[$i]$j", 100, "Number.MAX_VALUE", 0, 1, 1, 0);
							break;
							case 'real':
								$variable .= generaFormNumberField( "$atributs[$i]$j", "$atributs[$i]$j", 100, "Number.MAX_VALUE", 1, 1, 1, 0);
							break;
							default:
							break;
						}
					}
				}
			}
		}
			
		$variable .= "
		this.formulari = new Ext.FormPanel({
		bodyStyle: 'padding: 8px',
		url:'php/".$taula."Form.php',
		layout: 'form',
		labelWidth: 100,
		name: 'form',
		autoScroll: true,
		";
		for( $i=0; $i<count($atributs); $i++ ){
			if( substr ($atributs[$i], 0, 4) == "img_" or substr ($atributs[$i], 0, 5) == "file_" ){
				$variable .= "fileUpload: true,";
				break;
			}
		}
		$variable .= "
		items: 
		[this.accio
		";
		
		for( $i=0; $i<count($atributs); $i++ ){
			//PK o ID2 o IDIOMA
			if( $atributs[$i] == $pk or $atributs[$i] == "id2" or $atributs[$i] == "idioma" ){
				$variable .= ", this.$atributs[$i]";
			}else{
				//IMG
				if( substr ($atributs[$i], 0, 4) == "img_" or substr ($atributs[$i], 0, 5) == "file_" ){
					$variable .= ", this.$atributs[$i]";
				}else{
					//ALTRES
					for( $j=1; $j<=$idiomes; $j++ ){
						$variable .= ", this.$atributs[$i]$j";
					}
				}
			}
		}
		
		$variable .= "
		],
		buttonAlign: 'center',
		buttons: 
		[{
			text: BUTTON_cancel,
			scope: this,
			handler: function(){
				this.hide();
			}
		/*},{
			text: BUTTON_reset,
			scope: this,
			handler: function(){
				this.formulari.getForm().reset();
			}*/
		},{
			text: BUTTON_save,
			scope: this,
			handler: function() {
				var window = this;
				if (this.formulari.form.isValid()) {
					this.formulari.form.submit({
						waitMsg: AJAX_wait,
						failure: function(form, action) {
							Ext.MessageBox.alert(NOM_window_alert, ERROR_bbdd);
						},
						success: function(form, action) {
							Ext.Msg.alert(NOM_window_alert, SUCCESS_add, function(btn, text){
								if (btn == 'ok'){
									window.grid.store.reload();
									window.hide();
								}
							
							});
						}
					});
				}
				else {
					Ext.MessageBox.alert(NOM_window_alert, ERROR_obligatoris);
				}
			}	
		}]
	});
	
	".$taula."FormWindow.superclass.initComponent.apply(this, arguments);
	
	this.add(this.formulari);
	}
	
	/*,onRender:function() {
		".$taula."FormWindow.superclass.onRender.apply(this, arguments);
	  
	}*/

});
	
Ext.reg('".$taula."FormWindow', ".$taula."FormWindow);
";

return $variable;
}

?>